package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.ResultCode;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 医院设置表 服务实现类
 * </p>
 *
 *
 * @author atguigu
 * @since 2022-02-11
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {


    @Override
    public Page<HospitalSet> selectPage(Long page, Long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        Page<HospitalSet> pageParams = new Page<>(page, limit);

        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();

        //判断条件是否为空,为空则只做分页查询,如果不为空,则添加条件查询
        if (hospitalSetQueryVo == null) {
            //page方法有重载方法,可以接收queryWrapper
            //在service层,直接调用this  也可以选择使用baseMapper
            this.page(pageParams);
        } else {
            //queryWrapper提供的方法都有重载增加参数boolean condition 可以进行判断
            String hosname = hospitalSetQueryVo.getHosname();
            queryWrapper.like(!StringUtils.isEmpty(hosname), "hosname", hosname);
            String hoscode = hospitalSetQueryVo.getHoscode();
            queryWrapper.like(!StringUtils.isEmpty(hoscode), "hoscode", hoscode);

            this.page(pageParams, queryWrapper);
        }
        return pageParams;
    }

    @Override
    public Boolean updateHospitalSet(Long id,Integer status) {
        HospitalSet hospitalSet = new HospitalSet();
        //根据ID查找医院设置信息并设置状态
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        //调用方法
        boolean result = this.updateById(hospitalSet);
        return result;
    }

    /**
     * 根据医院编码 获取医院签名密钥
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if(hospitalSet == null) {
            throw new YyghException(ResultCode.ERROR, "医院信息不存在");
        }
        return hospitalSet.getSignKey();
    }
}
